<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
	<meta charset="UTF-8">
	<%@ include file="/WEB-INF/views/include/head.jsp"%>
    <link rel="stylesheet" href="${ctxStatic}/css/common_erp.css">
	<link rel="stylesheet" href="${ctxStatic}/css/layerSm.css">
	<script type="text/javascript">
	//获取 iframe 数量 
	var frLength = window.parent.frames.length;
	//父画面下标 
	var parentIndex = frLength-2;
	Math.formatFloat = function(f, digit) { 
		var s = f;
		//判断字符串是否为数字     //判断正整数 /^[1-9]+[0-9]*]*$/  
		var numberVerification = /^[0-9]+.?[0-9]*$/;   
		if (numberVerification.test(s)){
			s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(digit) + "";  
		} else {
			s = 0;
		}
	    return parseFloat(s); 
	}
	
	$(document).ready(function(){
 		var pIndex = $("#index").val();
		var parentDocument = window.parent.frames[parentIndex].document;
		var parentTr = $("#tableOrderMateriel tr[class^='parentTr']:eq("+pIndex+")",parentDocument);
		
		//物料图片
		$("#detailMaterialImg").attr('src',parentTr.find("td[class^='materialFile'] img").attr('src'));
		//面辅料编号
		$("#detailMaterialCode").text(parentTr.find("td[class^='materialCode']").text());
		//面辅料名
		$("#detailMaterialNameCn").text(parentTr.find("td[class^='materialNameCn']").text());
		//物料颜色
		$("#detailMaterialColor").text(parentTr.find("td[class^='materialColor']").text());
		//物料成分
		$("#detailMaterialComposition").text(parentTr.find("td[class^='materialComposition']").text());
		//幅宽
		$("#detailMaterialSpec").text(parentTr.find("td[class^='materialSpec']").text());
		var materialQuantity = parentTr.find("td[class^='materialQuantity']").text();
		//库存数量
		$("#detailMaterialQuantity").text(materialQuantity);
		formatMoneyThis($("#detailMaterialQuantity"), 2);
		//预定耗料数 
		$("#detailPlanMaterialLoss").text(parentTr.find("td[class^='cge-planMaterialLoss']").text());
		formatMoneyThis($("#detailPlanMaterialLoss"), 2);
		//核料明细
		$("#hiddenTable").html(parentTr.find("td[class^='addTable'] table tbody").html());
		<!--不可用库存数量 -->
		var unusableMQuantity = "${unusableMQuantity}";
		<!--可用库存数量 -->
		var usableMQuantity = Math.formatFloat((parseFloat(materialQuantity)-parseFloat(unusableMQuantity)),2);
		$("#detailUsableStockQuantity").text(usableMQuantity);
		formatMoneyThis($("#detailUsableStockQuantity"), 2);
		
		// 创建画面
		if ($("#onlyShow").val()!=null){
		  onClickTd();
		  inputSelect2();
	    }
		
		var parentTotalRowNum = parentTr.find("td[class^='totalRowNum']").text();
		if (Number(parentTotalRowNum)==0){
     		$("#materialAddRowIndex").val(0);
		} else {
			$("#materialAddRowIndex").val(parentTotalRowNum);		
		}  		
	});
	
	function addRow(){
		// add row
		/* var addRowIndex = $("#materialAddRowIndex").val(); */
		var addRowIndex = $("#hiddenTable tr").length;
 		var pIndex = $("#index").val();
		var parentDocument = window.parent.frames[parentIndex].document;
		var parentTr = $("#tableOrderMateriel tr[class^='parentTr']:eq("+pIndex+")",parentDocument);
		var sellen = $("#hiddenTable").find("select").length;
		var htmlParentTr = parentTr.find("td[class^='addRow'] table tbody").clone();
		var idTemp = "venderId"+addRowIndex;
		$(htmlParentTr).find("select")[0].id = idTemp;
 		$("#hiddenTable").append($(htmlParentTr).html());
 	    var hiddenTableLastTr = $("#hiddenTable").find("tr:last");
		hiddenTableLastTr.find("input:hidden[class^='subHiddenVenderId']").attr("name","orderMaterielList["+$("#index").val()+"].venderForm.materialPurchaseList["+addRowIndex+"].venderId");
		hiddenTableLastTr.find("input:hidden[class^='subHiddenPurchaseReserveQuantity']").attr("name","orderMaterielList["+$("#index").val()+"].venderForm.materialPurchaseList["+addRowIndex+"].purchaseReserveQuantity");
		hiddenTableLastTr.find("input:hidden[class^='subHiddenPurchaseUnitPrice']").attr("name","orderMaterielList["+$("#index").val()+"].venderForm.materialPurchaseList["+addRowIndex+"].purchaseUnitPrice");
		hiddenTableLastTr.find("input:hidden[class^='subHiddenPurchaseTaxRebate']").attr("name","orderMaterielList["+$("#index").val()+"].venderForm.materialPurchaseList["+addRowIndex+"].purchaseTaxRebate"); 
		$("#"+idTemp).select2();
 		onClickTd();
		$("#materialAddRowIndex").val(Number(addRowIndex)+1);
	}
	//格式化金额
    function formatMoneyThis(e, n) {  
    	var s=$(e).text().replace(",","").trim();
    	//判断字符串是否为数字     //判断正整数 /^[1-9]+[0-9]*]*$/  
    	var numberVerification = /^[0-9]+.?[0-9]*$/;   
        if (numberVerification.test(s)){
        	n = n > 0 && n <= 20 ? n : 2;  
            s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";  
            var l = s.split(".")[0].split("").reverse(), r = s.split(".")[1];  
            t = "";  
            for (var i = 0; i < l.length; i++) {  
                t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");  
            } 
            $(e).text(t.split("").reverse().join("") + "." + r);
        }else{
        	$(e).text("");
        }
    }  
	function onClickTd() {
		$("#hiddenTable").find("td[class^='sub-']").unbind("click"); 
	    $("#hiddenTable").find("td[class^='sub-']").on("click",function(){
	    	 //找到当前鼠标单击的td
	        var tdObj = $(this);
	        //保存原来的文本  
	        var oldText = $(this).text();
	        //创建一个文本框
	        var index = $(this).parent().index();
            var cellIndex = $(this)[0].cellIndex;
	        var inputTemp = "";
	        if (cellIndex==4){
	        	inputTemp = "<input type='text' value = '"+$(this).text()+"' maxlength = '6'>"
	        } else {
	        	inputTemp = "<input type='text' value = '"+$(this).text()+"' maxlength = '12'>";
	        }
	        var inputObj = $(inputTemp);
	        
	        //去掉文本框的边框  
	        inputObj.css("border-width", 0);
	        inputObj.click(function () {
	            return false;
	        });
	        //使文本框的宽度和td的宽度相同  
	        inputObj.width(tdObj.width()-4);
	        inputObj.height(tdObj.height()-15);
	        //去掉文本框的外边距 
	        inputObj.css("margin", 0);
	        inputObj.css("padding", 0);
	        //把文本框放到td中  
	        tdObj.html(inputObj);
	        //文本框失去焦点的时候变为文本  
	        inputObj.blur(function () {
		           var strRegex = /^(-?\d+)(\.\d+)?$/;
		           if (!strRegex.test($(this).val())){
		        	   $(this).val('0')
		           }
	            var newText = $(this).val();
	            if (cellIndex==2){
	            	if (parseInt(newText)>=1000000000){
	            		newText = 0;
	            	}
	            	newText = Math.formatFloat(newText,2);
            		$("#hiddenTable tr").eq(index).find(".subHiddenPurchaseReserveQuantity").val(newText);
	            }else if (cellIndex==3){
	            	if (parseInt(newText)>=1000000000){
	            		newText = 0;
	            	}
	            	newText = Math.formatFloat(newText,2);
	            	$("#hiddenTable tr").eq(index).find(".subHiddenPurchaseUnitPrice").val(newText);
	            }else if (cellIndex==4){
	            	if (parseInt(newText)>=100){
	            		newText = 0;
	            	}
	            	$("#hiddenTable tr").eq(index).find(".subHiddenPurchaseTaxRebate").val(newText);
	            }
	            tdObj.html(newText);
	            <!-- 单价  -->
	            var purchaseUnitPrice = $("#hiddenTable tr").eq(index).find(".sub-PurchaseUnitPrice").text();
	            <!-- 退税  -->
	            var purchaseTaxRebate = $("#hiddenTable tr").eq(index).find(".sub-PurchaseTaxRebate").text();
	            <!-- 数量   -->
	            var purchaseReserveQuantity = $("#hiddenTable tr").eq(index).find(".sub-PurchaseReserveQuantity").text();
	            <!-- 净单价  -->
	            var purchaseUnitCost;
 	            if (purchaseUnitPrice=="" || purchaseUnitPrice==null || purchaseUnitPrice=="undefined"){
	            	purchaseUnitPrice=0;
	            }
	            if (purchaseTaxRebate=="" || purchaseTaxRebate==null || purchaseTaxRebate== undefined || purchaseTaxRebate == "0" || purchaseTaxRebate == "0.00"){
	            	purchaseTaxRebate=0;
		            purchaseUnitCost = Math.formatFloat(parseFloat(purchaseUnitPrice),2);
	            } else {
	            	purchaseUnitCost = Math.formatFloat(parseFloat(purchaseUnitPrice)*parseFloat(purchaseTaxRebate),2);
				}
	            if (purchaseReserveQuantity=="" || purchaseReserveQuantity==null || purchaseReserveQuantity=="undefined"){
	            	purchaseReserveQuantity=0;
	            }
	            <!-- 金额  -->
	            var purchasePrice = Math.formatFloat(parseFloat(purchaseUnitCost)*parseFloat(purchaseReserveQuantity),2);
	            
	            // 净单价
	            $("#hiddenTable tr").eq(index).find(".PurchaseUnitCost").text(purchaseUnitCost);
	            // 金额
	            $("#hiddenTable tr").eq(index).find(".PurchasePrice").text(purchasePrice);
	        });
	        //全选  
	        inputObj.trigger("focus").trigger("select");
	   });
	}
		
	// 选择供货商
	function selectChange(obj){
		var index = $(obj).parent().parent().index();
        $("#hiddenTable tr").eq(index).find(".subHiddenVenderId").val($(obj).val());
        var index = $(obj).get(0).selectedIndex;
        $(obj).find("option").eq(index).attr("selected","true");
        var arr = $(obj).find("option");
     	$.each(arr, function(i, item){
     		if (i != index){
     			$(item).removeAttr("selected");
     		}
     	});
	}
	
    /* 删除入力信息  */
 	function removeVender(obj){  
            var tr=obj.parentNode.parentNode;  
            var tbody=tr.parentNode; 
            tbody.removeChild(tr); 
	 }
    
 	function addParent(){
 		// 供货商是否重复
        if (hasRepeat("hiddenTable",1)=="1"){
        	layer.msg('<spring:message code="orderMaterielDetail_list_purchase_vender_errmsg"/>', {time:1000});
        	return;
 		} 		
 		
 		// 单价，退税，数量不能为空
 		if (checkEmpty()=="true"){
 			return;
 		}
 		
		var pIndex = $("#index").val();
		var parentDocument = window.parent.frames[parentIndex].document;
		var parentTr = $("#tableOrderMateriel tr[class^='parentTr']:eq("+pIndex+")",parentDocument);
		$("#hiddenTable").find(".select2-container").remove();
		//核料明细
		parentTr.find("td[class^='addTable'] table tbody").html($("#hiddenTable").html());
		
		//选择行数
		parentTr.find("td[class^='totalRowNum']").text($("#materialAddRowIndex").val());
		
		var sum = 0;
		//采购数量计算
		var tempPurchaseReserveQuantity = $(this).find("td[class^='sub-PurchaseReserveQuantity']").text();
			
		$('#hiddenTable tr').each(function (i,e) {
			var selectObj = $(e).find("select")[0];
			if ($(selectObj).val() != "1" && $(selectObj).val() != "99") {
				  var tempPurchaseReserveQuantity = $(this).find("td[class^='sub-PurchaseReserveQuantity']").text();
				  if (tempPurchaseReserveQuantity=="" || tempPurchaseReserveQuantity==null || tempPurchaseReserveQuantity=="undefined"){
					  tempPurchaseReserveQuantity=0
				  }
				  sum = Math.formatFloat((sum + parseFloat(tempPurchaseReserveQuantity)),2);
			}
		});
		
		var findTd = parentTr.find("td[class^='sumInput']");
		findTd.text(sum);
		parentTr.find(".hiddenOrderMaterNum").val(sum);
		formatMoneyText($(findTd), 2);
		if ($('#hiddenTable tr').length>0){
			parentTr.find("td[class^='showDetails'] p").text('<spring:message code="orderMateriel_show_material_MaterielDetailExist"/>');
		} else {
			parentTr.find("td[class^='showDetails'] p").text('<spring:message code="orderMateriel_show_material_MaterielDetailNone"/>');
		}

		closeWin();
 	}
 	
 	function inputSelect2(){
    var selLen = $("#hiddenTable").find(".select2-chosen").length;
	$("#hiddenTable").find("select").select2();
 	}
 	
 	// 数量，单价，退税不能为空
 	function checkEmpty(){
 		var errFlg = "";
 		$('#hiddenTable tr').each(function () {
 		 var purchaseUnitPrice = $(this).find("td[class^='sub-PurchaseUnitPrice']").text();
         var purchaseTaxRebate = $(this).find("td[class^='sub-PurchaseTaxRebate']").text();
         var purchaseReserveQuantity = $(this).find("td[class^='sub-PurchaseReserveQuantity']").text();
         if (purchaseReserveQuantity == "" || purchaseReserveQuantity == null || purchaseReserveQuantity == "undefined" || purchaseReserveQuantity == 0){
        	 errFlg = "quantity";
             return false;
         }
          if (purchaseUnitPrice=="" || purchaseUnitPrice==null || purchaseUnitPrice=="undefined" || purchaseUnitPrice==0){
        	  errFlg = "price";
              return false;
          }
         if (purchaseTaxRebate=="" || purchaseTaxRebate==null || purchaseTaxRebate=="undefined" || purchaseTaxRebate==0){
        	 errFlg="rebate";
             return false;
         }
 		});

        if (errFlg=="quantity"){
        	// 数量
             layer.msg('<spring:message code="orderMaterielDetail_list_purchase_reserve_quantity_errmsg"/>', {time:1000});
             return "true";
        }else if (errFlg=="price"){
        	// 单价
              layer.msg('<spring:message code="orderMaterielDetail_list_purchase_price_errmsg"/>');
              return "true";
        }/* else if (errFlg=="rebate"){
        	// 退税
             layer.msg('<spring:message code="orderMaterielDetail_list_purchase_tax_rebate_errmsg"/>', {time:1000});
             return "true";
        } */
        return "false";
 	}
 	
 	
 	function hasRepeat(objId,columnIndex){ 
 		var arr = []; 
 		$('#hiddenTable tr').each(function () {
 		   arr.push($(this).find(".subHiddenVenderId").val()); 
 		}); 
 		if (arr.length==$.unique(arr).length){ 
 		  return false; 
 		} else { 
 		  return true; 
 		} 
 		} 
    </script>
</head>
<body>
<div class="layerHeader clearfix">	   
     <p class="headerTitle"><spring:message code="orderMaterielDetail_head"/></p>	
    <div class="headerButton clearfix">
        <button type="button" onclick="popup('600px','324px','${ctx}/customization/nuclearmaterial/purchasingResume?materialId=${orderMateriel.materialId}')"><spring:message code="orderMaterielDetail_btn_record"/></button>
        <c:if test="${orderMateriel.onlyShow==null}">
        <button type="button" onclick="addParent();"><spring:message code="btn_ok"/></button>
        </c:if>
        <button type="button" onclick="closeWin();"><spring:message code="btn_cancle"/></button>
    </div>
</div>
<form id="inputForm"  method="post">
<input type="hidden" value="${orderMateriel.index}" id="index" name="index">
<input type="hidden" value="${orderMateriel.materialId}" id="materialId" name="materialId">
<input type="hidden" value="${orderMateriel.onlyShow}" id="onlyShow" name="onlyShow">
<!--添加行下标-->
<input type="hidden" id="materialAddRowIndex" name="materialAddRowIndex" value="0">
<div class="tabBoxSm" >
  <div class="tabSm smOrderBox">  
        <p class="title"><spring:message code="orderMaterielDetail_info"/></p>
        <div class="clearfix">
            <div class="col-33">
               <img id="detailMaterialImg" style="width:90px;height:90px;" src="">
            </div>
        	<div class="col-66">
            	<div class="items clearfix">        	
                    <div class="col-33 clearfix">
                        <p class="tit"><spring:message code="orderMaterielDetail_materialCode"/></p>
                        <p id="detailMaterialCode"></p>
                    </div>
                    <div class="col-33 clearfix">
                        <p class="tit"><spring:message code="orderMaterielDetail_materialNameCn"/></p>
                        <p id="detailMaterialNameCn"></p>
                    </div>
                    <div class="col-33 clearfix">
                        <p class="tit"><spring:message code="orderMaterielDetail_materialSpec"/></p>
                        <p id="detailMaterialSpec"></p>
                    </div>
                </div>
                <div class="items clearfix">        	
                    <div class="col-33 clearfix">
                        <p class="tit"><spring:message code="orderMaterielDetail_materialColor"/></p>
                        <p id="detailMaterialColor"></p>
                    </div>
                    <div class="col-33 clearfix">
                        <p class="tit"><spring:message code="orderMaterielDetail_materialCompositionCd"/></p>
                        <p id="detailMaterialComposition"></p>
                    </div>
                </div>
                <div class="items clearfix">     
                	<div class="col-33 clearfix">
                        <p class="tit"><spring:message code="orderMaterielDetail_stockQuantity"/></p>
                        <p id="detailMaterialQuantity" style="color: red;"></p>
                    </div>   	
                    <div class="col-33 clearfix">
                        <p class="tit" style="width: 80px;"><spring:message code="orderMaterielDetail_usableStockQuantity"/></p>
                        <p id="detailUsableStockQuantity" style="color: red;"></p>
                    </div>
                    <div class="col-33 clearfix">
                        <p class="tit"><spring:message code="orderMaterielDetail_planMaterialLoss"/></p>
                        <p id="detailPlanMaterialLoss" style="color: red;"></p>
                    </div>
                </div>
            </div>
        </div>    
    <!-- 核料明细 -->
    <div class="items clearfix">
    <p class="title" style="margin-top:0;margin-bottom:5px;margin-left:10px;"><spring:message code="orderMaterielDetail_list"/>
        <c:if test="${orderMateriel.onlyShow==null}">
        &ensp;<img src="${ctxStatic}/images/btn_add.png" style="width:25px;height:25px;" onclick='addRow()'>
        </c:if>
    </p>
    </div>
    <div class="tableArea">
		<div class="tableScroll tableScrollSm" style="overflow:auto;height:212px;width:705px">
			<table cellpadding="0" class="tableHead" style="z-index: 9999">
	            <thead>
	            <tr>
	                <!-- 操作 -->
	            	<th style="width:50px"><spring:message code="orderMaterielDetail_list_operation"/></th>
	            	<!-- 供货商 -->
	                <th style="width:156px"><spring:message code="orderMaterielDetail_list_supplier"/></th>
	                <!-- 数量 -->
	                <th style="width:95px"><spring:message code="orderMaterielDetail_list_purchase_reserve_quantity"/></th>
	                <!-- 单价 -->
	                <th style="width:95px"><spring:message code="orderMaterielDetail_list_purchase_price"/></th>
	                <!-- 退税 -->
	                <th style="width:95px"><spring:message code="orderMaterielDetail_list_purchase_tax_rebate"/></th>
	                <!-- 净单价 -->
	                <th style="width:95px"><spring:message code="orderMaterielDetail_list_purchase_unit_price"/></th>
	                <!-- 金额 -->
	                <th style="width:95px"><spring:message code="orderMaterielDetail_list_purchase_amount"/></th>
	            </tr>
	            </thead>
	        </table>
	        <table cellpadding="0" class="tableBody">
	            <tbody id="hiddenTable">
	            </tbody>
	        </table>
	    </div>
    </div>
   </div> 
 </div>
 </form>
</body>
</html>
